package com.oa.srms.apps.business.goods.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.business.goods.IGoodInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.goods.IGoodInfoDao;
import com.oa.srms.apps.entity.goods.GoodInfoValue;

public class GoodInfoServiceImpl implements IGoodInfoService {

	@Autowired
	private GoodInfoValue goodInfo;
	@Autowired
	private IGoodInfoDao goodInfoDao;

	@Override
	public Integer addGoodInfo(String goodNo, String goodName,
			Integer goodNum) throws Exception {
		goodInfo.setGoodNo(goodNo);
		goodInfo.setGoodName(goodName);
		goodInfo.setGoodNum(goodNum);
		goodInfo.setStates(IDEDStates.ENABLE);
		return goodInfoDao.addGoodInfo(goodInfo);
	}

	@Override
	public void modifyGoodInfo(Integer id, String goodNo,
			String goodName, Integer goodNum) throws Exception {
		goodInfo = this.queryById(id);
		if (goodInfo != null) {
			goodInfo.setGoodNo(goodNo);
			goodInfo.setGoodName(goodName);
			goodInfo.setGoodNum(goodNum+goodInfo.getGoodNum());
			goodInfoDao.editGoodInfo(goodInfo);
		}
	}

	@Override
	public void deleteGoodInfo(Integer id) throws Exception {
		goodInfo = goodInfoDao.queryById(id);
		
		goodInfo.setStates(IDEDStates.DELETE);
		
		goodInfoDao.editGoodInfo(goodInfo);
	}

	@Override
	public GoodInfoValue queryById(Integer id) throws Exception {
		return goodInfoDao.queryById(id);
	}

	@Override
	public List<GoodInfoValue> queryGoodInfo(String goodName, Integer states,
			Integer rows, Integer pageNo) throws Exception {
		goodInfo.setGoodName(goodName);
		goodInfo.setStates(states);
		return goodInfoDao.queryGoodInfo(goodInfo, rows, pageNo);
	}

	@Override
	public Integer getTotalPageNumber(String goodName, Integer states, Integer rows) throws Exception {
		goodInfo.setGoodName(goodName);
		goodInfo.setStates(states);
		return goodInfoDao.getTotalPageNumber(goodInfo, rows);
	}

	@Override
	public Integer getTotalNumber() {
		return goodInfoDao.getTotalNumber();
	}

	@Override
	public void toEnable(Integer id) throws Exception {
		goodInfo = goodInfoDao.queryById(id);
		
		goodInfo.setStates(IDEDStates.ENABLE);
		
		goodInfoDao.editGoodInfo(goodInfo);
		
	}

	@Override
	public void toDisable(Integer id) throws Exception {
		goodInfo = goodInfoDao.queryById(id);
		
		goodInfo.setStates(IDEDStates.DISABLE);
		
		goodInfoDao.editGoodInfo(goodInfo);
		
	}

	@Override
	public List<GoodInfoValue> queryGoodInfo(Integer states) throws Exception {
		return goodInfoDao.queryGoodInfo(states);
	}

	@Override
	public void toSubNum(Integer id) throws Exception {
		goodInfo = goodInfoDao.queryById(id);
		
		goodInfo.setGoodNum(goodInfo.getGoodNum()-1);
		
		goodInfoDao.editGoodInfo(goodInfo);
		
	}



}
